<?php
 
/* setting zona waktu */
date_default_timezone_set('Asia/Jakarta');
 
/* konstruktor halaman pdf sbb :    
   P  = Orientasinya "Potrait"
   cm = ukuran halaman dalam satuan centimeter
   A4 = Format Halaman
    
   jika ingin mensetting sendiri format halamannya, gunakan array(width, height)  
   contoh : $this->fpdf->FPDF("P","cm", array(20, 20));  
*/
 
$this->fpdf->FPDF("P","cm","A4");
 
// kita set marginnya dimulai dari kiri, atas, kanan. jika tidak diset, defaultnya 1 cm
$this->fpdf->SetMargins(1,1,1);
$this->fpdf->SetAutoPageBreak(false, 0.5); 
 
/* AliasNbPages() merupakan fungsi untuk menampilkan total halaman
   di footer, nanti kita akan membuat page number dengan format : number page / total page
*/
$this->fpdf->AliasNbPages();
 
// AddPage merupakan fungsi untuk membuat halaman baru
$this->fpdf->AddPage();
 
// Setting Font : String Family, String Style, Font size 
$this->fpdf->SetFont('Arial','B',12);
 
/* Kita akan membuat header dari halaman pdf yang kita buat 
   -------------- Header Halaman dimulai dari baris ini -----------------------------
*/
$this->pdf_header_p();



/* -------------- Header Halaman selesai ------------------------------------------------*/
 
$this->fpdf->Ln(0);
$this->fpdf->SetFont('Arial','B',16);
$this->fpdf->Cell(19,1,'INVOICE',0,0,'C');
 
 
/* setting header table */
$this->fpdf->Ln(2);

$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'No. Invoice ' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(7 , 1, $g['resv_id'] , 0, 'LR', 'L'); 
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'Adult ' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(5 , 1, $g['resv_adult_number'] , 0, 'LR', 'L'); 
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'EMP' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(7 , 1, $g['emp_name'] , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'Child ' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(5 , 1, $g['resv_child_number'] , 0, 'LR', 'L');  
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'Guest Name ' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(7 , 1, $g['guest_name'] , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'Infant' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->fpdf->Cell(5 , 1, $g['resv_infant_number'] , 0, 'LR', 'L'); 
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(3 , 1, 'Address ' , 0, 'LR', 'L');
$this->fpdf->SetFont('Arial','',11);
$this->pdf_cell(17 , 1, $g['guest_address'] , 0, 'L'); 


/*Table Penjelasan Reservation*/
$this->fpdf->Ln(2);
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->SetFillColor(216,216,216);
$this->fpdf->Cell(5 , 1, 'Reservation Room' , 0, 'LR', 'L');
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->Cell(3 , 1, 'Room Number' , 1, 'LR', 'C',true);
$this->fpdf->Cell(4, 1, 'Booking' , 1, 'LR', 'C',true);
$this->fpdf->Cell(4 , 1, 'Check In' , 1, 'LR', 'C',true);
$this->fpdf->Cell(4 , 1, 'Check Out' , 1, 'LR', 'C',true);
$this->fpdf->Cell(3 , 1, 'Price' , 1, 'LR', 'C',true);

foreach($ro as $row )
{
	$this->fpdf->Ln();
    $this->fpdf->SetFont('Arial','',10);
	$this->fpdf->Cell(3 , 0.7, $row['room_number' ], 1, 'LR', 'C');
	$this->fpdf->Cell(4 , 0.7, $row['booking' ], 1, 'LR', 'C');
	$this->fpdf->Cell(4 , 0.7, $row['check_in' ], 1, 'LR', 'C');
	$this->fpdf->Cell(4 , 0.7, $row['check_out' ], 1, 'LR', 'C');
	$this->fpdf->Cell(3 , 0.7,  'Rp. '.number_format($row['price' ],2,',','.'), 1, 'LR', 'R');
	
	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}
}
if(count($m) > 0){
	$this->fpdf->Ln(1);
	$this->fpdf->SetFont('Arial','B',11);
	$this->fpdf->Cell(5 , 1, 'Room Service' , 0, 'LR', 'L');
	$this->fpdf->Ln();
	$this->fpdf->SetFont('Arial','B',10);
	$this->fpdf->Cell(4 , 1, 'Waktu Pemesanan' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(2, 1, ' Code' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Description' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(2 , 1, 'Qty' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Harga' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Total' , 1, 'LR', 'C',true);
}
	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

foreach($m as $row)
{

$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Cell(4 , 0.7, $row['mat_time'] , 1, 'LR', 'C');
$this->fpdf->Cell(2, 0.7, $row['material_code'], 1, 'LR', 'C');
$this->fpdf->Cell(3 , 0.7, $row['material_name'] , 1, 'LR', 'C');
$this->fpdf->Cell(2 , 0.7, number_format($row['mat_qty'],2,',','.'), 1, 'LR', 'C');
$this->fpdf->Cell(3 , 0.7, 'Rp. '.number_format($row['resv_mat_price' ],2,',','.'), 1, 'LR', 'R');
$this->fpdf->Cell(3 , 0.7, 'Rp. '.number_format($row['pricematerial' ],2,',','.'), 1, 'LR', 'R');
	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}
}

if(count($s) > 0){
	$this->fpdf->Ln(1);
	$this->fpdf->SetFont('Arial','B',11);
	$this->fpdf->Cell(5 , 1, 'Service' , 0, 'LR', 'L');
	$this->fpdf->Ln();
	$this->fpdf->SetFont('Arial','B',10);
	$this->fpdf->Cell(4 , 1, 'Waktu Pemesanan' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Room' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(1.5, 1, 'Code' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Description' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(2 , 1, 'Qty' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Harga' , 1, 'LR', 'C',true);
	$this->fpdf->Cell(3 , 1, 'Total' , 1, 'LR', 'C',true);
}

	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

foreach($s as $row)
{

$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Cell(4 , 0.7, $row['tanggal']  , 1, 'LR', 'C');
$this->fpdf->Cell(3 , 0.7, $row['room_name']  , 1, 'LR', 'C');
$this->fpdf->Cell(1.5, 0.7, $row['service_code']  , 1, 'LR', 'C');
$this->fpdf->Cell(3 , 0.7, $row['service_name'] , 1, 'LR', 'C');
$this->fpdf->Cell(2 , 0.7, number_format($row['qty'],2,',','.'), 1, 'LR', 'C');
$this->fpdf->Cell(3 , 0.7, 'Rp. '.number_format( $row['serv_det_price'],2,',','.'), 1, 'LR', 'R');
$this->fpdf->Cell(3 , 0.7,'Rp. '.number_format(  $row['total'],2,',','.') , 1, 'LR', 'R');
	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

}

	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}
if(count($s) > 0){
$this->fpdf->Ln();
$this->fpdf->Cell(10.5 , 0.7, '' , 0, 'LR', 'C'); 
$this->fpdf->Cell(4 , 0.7, 'Sub Total Service' , 0, 'LR', 'L'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($t['subtotal'],2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln();
$this->fpdf->Cell(10.5 , 0.7, '' , 0, 'LR', 'C'); 
$this->fpdf->Cell(3 , 0.7, 'Discount' , 0, 'LR', 'L');
}
$diskon = 0;
$newtot = 0;
$service = 0;
$ppn = 0;
$grandservice = 0;

if(count($s) > 0){
if($t['discount_percent'] != null){
$diskon = ($t['discount_percent'] * $t['subtotal'])/100;
$this->fpdf->Cell(1 , 0.7, number_format($t['discount_percent'],2,',','.').'%' , 0, 'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($diskon,2,',','.') , 0, 'LR', 'R');
 $newtot = $t['subtotal'] - $diskon;
 $service =($newtot * $t['service_tax']) /100 ;
  $ppn =($newtot * $t['ppn_tax']) /100 ;
  $grandservice = $newtot + $service + $ppn;
  
 }elseif($t['discount_nominal'] != null){
 
 $diskon = $t['discount_nominal'];
 $this->fpdf->Cell(1 , 0.7,'',0 ,'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($diskon,2,',','.') , 0, 'LR', 'R');
  $newtot = $t['subtotal'] - $diskon;
  $service =($newtot * $t['service_tax']) /100 ;
  $ppn =($newtot * $t['ppn_tax']) /100 ;
  $grandservice = $newtot + $service + $ppn;
  
 }elseif($t['discount_nominal'] == null && $t['discount_percent'] == null )
 {
 $service =($t['subtotal'] * $t['service_tax']) /100 ;
  $ppn =($t['subtotal'] * $t['ppn_tax']) /100 ;
  $grandservice = $t['subtotal'] + $service + $ppn;
  }
  
 $this->fpdf->Ln();
 $this->fpdf->Cell(10.5 , 0.7, '' , 0, 'LR', 'C'); 
$this->fpdf->Cell(3 , 0.7, 'Service Tax' , 0, 'LR', 'L');
$this->fpdf->Cell(1 , 0.7, number_format($t['service_tax'],2,',','.').'%' , 0, 'LR', 'R');  
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($service,2,',','.') , 0, 'LR', 'R');
 $this->fpdf->Ln();
 $this->fpdf->Cell(10.5 , 0.7, '' , 0, 'LR', 'C'); 
$this->fpdf->Cell(3 , 0.7, 'PPN Tax' , 0, 'LR', 'L');
$this->fpdf->Cell(1 , 0.7, number_format($t['ppn_tax'],2,',','.').'%' , 0, 'LR', 'R');  
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($ppn,2,',','.') , 0, 'LR', 'R');
 $this->fpdf->Ln();
 $this->fpdf->Cell(10.5 , 0.7, '' , 0, 'LR', 'C'); 
$this->fpdf->Cell(4 , 0.7, 'Total Service Cost' , 0, 'LR', 'L');
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($grandservice,2,',','.') , 0, 'LR', 'R');
}
 
	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

$this->fpdf->Ln(1);
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(5 , 1, 'Grand Total' , 0, 'LR', 'L');
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Cell(5 , 0.7, 'Total Reservation Cost' , 0, 'LR', 'L'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($tr['subtotalroom'],2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln();
$this->fpdf->Cell(5 , 0.7, 'Total Room Service Cost' , 0, 'LR', 'L'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($tm['subtotmat'],2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln();
$this->fpdf->Cell(4 , 0.7, 'Extra Charge' , 0, 'LR', 'L');

$subtot = $tr['subtotalroom'] + $tm['subtotmat'] ;
$diskontot = 0;
$newsub = 0;
$newtot = 0;
$servicesub = 0;
$ppnsub = 0;
$grandtot = 0;

if($tr['extra_charge_percent'] != null ){

$charge = ($tr['extra_charge_percent'] * $subtot)/100;
$this->fpdf->Cell(1 , 0.7, number_format($tr['extra_charge_percent'],2,',','.').'%' , 0, 'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($charge,2,',','.') , 0, 'LR', 'R');
 $newsub = $subtot + $charge;

}elseif($tr['extra_charge_nominal'] != null){

$charge = $tr['extra_charge_nominal'];
$this->fpdf->Cell(1 , 0.7, '', 0, 'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($charge,2,',','.') , 0, 'LR', 'R');
 $newsub = $subtot + $charge;

}elseif($tr['extra_charge_nominal'] == null && $tr['extra_charge_percent'] == null )
 {
	$this->fpdf->Cell(1 , 0.7,'',0, 'LR', 'R'); 
	$this->fpdf->Cell(4 , 0.7, '', 0, 'LR', 'R');
	$newsub = $subtot;
  }

$this->fpdf->Ln();
$this->fpdf->Cell(4 , 0.7, 'Discount' , 0, 'LR', 'L');

if($tr['discount_percent'] != null){
$diskontot = ($tr['discount_percent'] * $newsub)/100;
$this->fpdf->Cell(1 , 0.7, number_format($tr['discount_percent'],2,',','.').'%' , 0, 'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($diskontot,2,',','.') , 0, 'LR', 'R');
 $newtot = $newsub - $diskontot;

  
 }elseif($tr['discount_nominal'] != null){
 
 $diskontot = $tr['discount_nominal'];
 $this->fpdf->Cell(1 , 0.7,'',0, 'LR', 'R'); 
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($diskontot,2,',','.') , 0, 'LR', 'R');
  $newtot = $newsub - $diskontot;

  
 }elseif($tr['discount_nominal'] == null && $tr['discount_percent'] == null )
 {
	$this->fpdf->Cell(1 , 0.7,'',0, 'LR', 'R'); 
	$this->fpdf->Cell(4 , 0.7, '', 0, 'LR', 'R');
	$newtot = $newsub;
  }
   $servicesub =($newtot * $tr['service_tax']) /100 ;
  $ppnsub =($newtot * $tr['ppn_tax']) /100 ;
  $grandtot = $newtot + $servicesub + $ppnsub + $grandservice;
$this->fpdf->Ln();
$this->fpdf->Cell(4 , 0.7, 'Service Tax' , 0, 'LR', 'L');
$this->fpdf->Cell(1 , 0.7, number_format($tr['service_tax'],2,',','.').'%' , 0, 'LR', 'R');  
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($servicesub,2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln(); 
$this->fpdf->Cell(4 , 0.7, 'PPN Tax' , 0, 'LR', 'L');
$this->fpdf->Cell(1 , 0.7, number_format($tr['ppn_tax'],2,',','.').'%' , 0, 'LR', 'R');  
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($ppnsub,2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln();
$this->fpdf->Cell(5 , 0.7, 'Total Service Cost' , 0, 'LR', 'L');
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($grandservice,2,',','.') , 0, 'LR', 'R');
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->Cell(5 , 0.7, 'Grand Total' , 0, 'LR', 'L');
$this->fpdf->Cell(4 , 0.7, 'Rp. '.number_format($grandtot,2,',','.') , 0, 'LR', 'R');

	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

$this->fpdf->Ln(1);
$this->fpdf->SetFont('Arial','B',11);
$this->fpdf->Cell(5 , 1, 'Transaction' , 0, 'LR', 'L');
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->Cell(3 , 1, 'Payment Type' , 1, 'LR', 'C',true);
$this->fpdf->Cell(5 , 1, 'Amount' , 1, 'LR', 'C',true);
$this->fpdf->Cell(3 , 1, 'Description' , 1, 'LR', 'C',true);
$this->fpdf->Cell(5 , 1, 'Voucher/Card Code' , 1, 'LR', 'C',true);
foreach($ts as $row)
{

$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Cell(3 , 0.7, $row['payment_name']  , 1, 'LR', 'C');
$this->fpdf->Cell(5 , 0.7, 'Rp. '.number_format(  $row['resv_paym_total'],2,',','.')  , 1, 'LR', 'R');
$this->fpdf->Cell(3, 0.7, $row['resv_paym_desc']  , 1, 'LR', 'C');
$this->fpdf->Cell(5 , 0.7, $row['special_code'] , 1, 'LR', 'C');

	if($this->fpdf->GetY() >= 20) {
		$this->pdf_footer_p();
		$this->fpdf->AddPage('P', 'A4');
		$this->pdf_header_p();
	}

}
$balanced = $grandtot - $tt['total'];
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->Cell(3 , 1, 'Balanced' , 1, 'LR', 'C');
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Cell(5 , 1, 'Rp. '.number_format(  $balanced,2,',','.') , 1, 'LR', 'R');
$this->fpdf->Cell(3 , 1, '' , 1, 'LR', 'C');
$this->fpdf->Cell(5 , 1, '' , 1, 'LR', 'C');
/* setting posisi footer 3 cm dari bawah */
$this->pdf_footer_p();
 
/* generate pdf jika semua konstruktor, data yang akan ditampilkan, dll sudah selesai */
$this->fpdf->Output("invoice.pdf","I");
?>